class Solution {
public:
    vector<string> ret;
    string path;
    int count_left = 0;
    int count_right = 0;
    void dfs(int n,int len)
    {
        if(2*n == len&&count_left == count_right)
        {
            ret.push_back(path);
            return;
        }
        if(count_left<n)
        {
            path.push_back('(');
            count_left++;
            dfs(n,len+1);
            count_left--;
            path.pop_back();//恢复现场
        }
        if(count_left>count_right)
        {
            path.push_back(')');
            count_right++;
            dfs(n,len+1);
            count_right--;
            path.pop_back();
        }
    }
    vector<string> generateParenthesis(int n) 
    {
        dfs(n,0);
        return ret;
    }
};